package dao;

import domain.User;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 用户的dao，访问数据库完成数据增加和查找
 */
public class UserDao {

    public boolean exist(String username){
        /*
        判断注册时输入的用户名是否已经存在
         */
        boolean exist = false;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // 获得数据的连接
            conn = JDBCUtils.getConnection();
            // 获得Statement对象
            stmt = conn.createStatement();
            // 发送SQL语句
            String sql = "SELECT * FROM users WHERE name='"+username+"'";
            rs = stmt.executeQuery(sql);
            // 处理结果集
            if (rs.next())
                exist = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.release(rs, stmt, conn);
        }
        return exist;
    }

    public boolean insert(User user) {
        /*
        添加用户的操作
         */
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = JDBCUtils.getConnection();
            stmt = conn.createStatement();
            String sql = "INSERT INTO users(name,password) VALUES('"+user.getUsername()+"','"+
                    user.getPassword()+"')";
            int num = stmt.executeUpdate(sql);
            return num > 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.release(stmt, conn);
        }
        return false;
    }

    public User login(String username, String password) {
        /*
        注册用户登录验证
         */
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        User user = null;
        try {
            conn = JDBCUtils.getConnection();
            stmt = conn.createStatement();
            String sql = "SELECT * FROM users WHERE name='"+username+"' AND password='"+password+"'";
            rs = stmt.executeQuery(sql);
            if (rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                user.setRole(rs.getString("role"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.release(rs, stmt, conn);
        }
        return user;
    }

    public String search(int id){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            stmt = conn.createStatement();
            String sql = "SELECT * FROM users WHERE id='"+id+"'";
            rs = stmt.executeQuery(sql);
            if (rs.next()){
                return rs.getString("name");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.release(rs, stmt, conn);
        }
        return null;
    }
}
